const path = require('path')
const webpack = require('webpack')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
// const { config } = require('process')

// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
// const { CleanWebpackPlugin } = require('clean-webpack-plugin')
// const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
  entry: './src/main.js',
  output: {
    filename: process.env.NODE_ENV === 'development' ? '[name].bundle.js' : '[name]-[chunkhash:8].js',
    path: path.join(__dirname, 'dist')
  },
  mode: process.env.NODE_ENV,
  devtool: process.env.NODE_ENV === 'development' ? 'cheap-module-eval-source-map' : 'none',
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: 'vue-loader'
      },
      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader'
        },
        exclude: /node_modules/
      },
      {
        test: /\.js$/,
        use: {
          loader: 'eslint-loader'
        },
        enforce: 'pre',
        include: path.join(__dirname, 'src')
      },
      {
        test: /\.(less|css)$/,
        use: [
          MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'
        ]
      },
      {
        test: /\.(png|jpe?g|gif)$/,
        use: {
          loader: 'url-loader',
          options: {
            esModule: false,
            limit: 5 * 1024,
            name: '[name]-[hash:8].[ext]'
          }
        }
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: {
          loader: 'file-loader',
          options: {
            name: '[name]-[hash:8].[ext]'
          }
        }
      }
    ]
  },
  resolve: {
    alias: {
      '@components': path.join(__dirname, 'src/components'),
      '@assets': path.join(__dirname, 'src/assets'),
      vue$: 'vue/dist/vue.esm.js'
    },
    extensions: [
      '.js',
      '.jsx',
      '.vue',
      '.json'
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      title: 'webpack作业',
      meta: {
        viewport: 'width=device-width'
      },
      favicon: './public/favicon.ico',
      template: './public/index.html'
    }),
    new webpack.DefinePlugin({
      BASE_URL: `'${__dirname}/'`
    }),
    new MiniCssExtractPlugin()
    // new OptimizeCssAssetsWebpackPlugin(),
    // new UglifyJsPlugin()
  ],
  optimization: {
    usedExports: true,
    minimize: true,
    minimizer: [
      new OptimizeCssAssetsWebpackPlugin(),
      new TerserWebpackPlugin({
        // 开启缓存
        cache: true,
        // 开启多进程打包
        parallel: true
      })
    ],
    concatenateModules: true,
    splitChunks: {
      // chunks: 'all'
      // minSize: 30000,
      cacheGroups: {
        vendor: {
          name: 'chunk-vendors',
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        },
        common: {
          name: 'chunk-common',
          minChunks: 2,
          priority: -20,
          chunks: 'initial',
          reuseExistingChunk: true
        },
        element: {
          chunks: 'all',
          name: 'element-ui',
          test: /[\\/]element-ui[\\/]/,
          priority: 0
        }
      }
    }
  }
}
